import time

from selenium.webdriver.common.by import By

from Pages import BasePage


class tripPage(BasePage.Action):

    def __init__(self):
        #提交行程
        self.submitTrip_xpath = (By.XPATH,"//button[text() = '提交行程']")
        #行程提交页获取下单的机票订单号
        self.flightOrderNo_xpath = (By.XPATH, "//span[contains(text(),'DF')]")
        #行程提交页获取下单的国际机票订单号
        self.int_flightOrderNo_xpath = (By.XPATH, "//span[contains(text(),'IF')]")
        #行程提交页获取下单的火车票订单号
        self.trainOrderNo_xpath = (By.XPATH, "//span[contains(text(),'DT')]")
        #行程提交页获取下单的酒店订单号
        self.hotelOrderNo_xpath = (By.XPATH, "//span[contains(text(),'HO')]")
        #关闭温馨提示
        self.tips_xpath = (By.XPATH, "//div[@class='title' and text()='温馨提示']")
        self.closeButton_xpath = (By.XPATH, "//label[@class='iconfont icon-guanbi2']")
        #需审行程，提交审批按钮
        self.submitApproval_xpath = (By.XPATH, "//button[@class='approval']")
        #确认提交按钮
        self.confirmSubmission_xpath = (By.XPATH, "//button[text() = '确认提交']")

    def submitTrip(self):
        self.closeTips()
        #提交行程按钮，免审+无行程提交项 流程结束
        self.smart_click(*self.submitTrip_xpath)
        time.sleep(1)
        #需审+无行程提交项 流程
        self.smart_click(*self.submitApproval_xpath)
        time.sleep(2)
        #免审+行程提交项非必填
        self.smart_click(*self.confirmSubmission_xpath)
        time.sleep(2)

    def goDFDetail(self):
        self.smart_click(*self.flightOrderNo_xpath)
        self.switch_window()
        time.sleep(3)

    def goIFDetail(self):
        self.smart_click(*self.int_flightOrderNo_xpath)
        self.switch_window()
        time.sleep(3)

    def goDTDetail(self):
        self.find_element(*self.trainOrderNo_xpath).click()
        time.sleep(3)
        self.switch_window()
        time.sleep(3)

    def closeTips(self):
        if self.find_element(*self.tips_xpath):
            self.smart_click(*self.closeButton_xpath)
            time.sleep(1)

    def checkHotelAndSubmint(self, hotelName):
        #行程信息
        tripInfo_xpath = (By.XPATH, "//p[@title='%s']" % hotelName)
        if self.find_element(*tripInfo_xpath):
            hotelOrderNo = self.find_element(*self.hotelOrderNo_xpath).get_attribute("title")
            self.submitTrip()
            time.sleep(5)
            return hotelOrderNo
        else:
            print("酒店信息未匹配，不提交行程！")
